package com.dengdai.applibrary.fragments;

import android.view.View;
import android.widget.ExpandableListView;

import com.dengdai.applibrary.BaseApplication;
import com.dengdai.applibrary.activitys.BaseActivity;
import com.dengdai.applibrary.adapter.BaseCommExpandableListAdapter;
import com.dengdai.applibrary.config.ResIdName;
import com.dengdai.applibrary.config.ViewInfo;
import com.dengdai.applibrary.interfaces.ExpandableListInterface;
import com.dengdai.applibrary.utils.BaseViewUtils;
import com.dengdai.applibrary.utils.NetWorkUtils;
import com.dengdai.applibrary.widgets.custom.LoadDataEmptyView;
import com.dengdai.pullrefresh.library.PullToRefreshBase;
import com.dengdai.pullrefresh.library.PullToRefreshExpandableListView;

/**
 * @说明 折叠分组列表
 * @作者 LY
 * @时间 16/5/24 09:56
 * @邮箱 2743569843@qq.com
 * @版权 Copyright(c) 2016 LY-版权所有
 * @备注
 */
public abstract class BaseExpandableListFragment extends BaseFragment implements ExpandableListInterface {
    protected PullToRefreshExpandableListView pullExpandableListView;
    protected LoadDataEmptyView loadDataView;
    protected BaseCommExpandableListAdapter baseCommExpandableListAdapter;
    private View headerView;
    private View footerView;
    protected int page = 0;

    @Override
    public int getLayoutResID() {
        return BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.LAYOUT, ViewInfo.BASE_EXPANDABLE_LIST_FRAGMENT);
    }

    @Override
    public void initData(View view) {
        pullExpandableListView = (PullToRefreshExpandableListView) view.findViewById(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.ID, ResIdName.PULLEXPANDABLELISTVIEW));
        loadDataView = (LoadDataEmptyView) view.findViewById(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.ID, ResIdName.LOADDATAEMPTY));
    }

    @Override
    public void business() {
        baseCommExpandableListAdapter = getAdapter();
        pullExpandableListView.getRefreshableView().setAdapter(baseCommExpandableListAdapter);
        //设置默认页码
        page = getResources().getInteger(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.INTEGER, ResIdName.DEFAULT_PAGE));
        //设置加载模式
        if (getListViewMode() != null) {
            pullExpandableListView.setMode(getListViewMode());
        } else {
            pullExpandableListView.setMode(PullToRefreshBase.Mode.BOTH);
        }
        //设置折叠指示器
        pullExpandableListView.getRefreshableView().setGroupIndicator(getGroupIndicator());
        //设置头部布局
        headerView = getHeaderView();
        if (headerView != null) {
            pullExpandableListView.getRefreshableView().addHeaderView(headerView);
        }
        //设置底部布局
        footerView = getFooterView();
        if (footerView != null) {
            pullExpandableListView.getRefreshableView().addFooterView(footerView);
        }
        //设置刷新和加载
        pullExpandableListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ExpandableListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ExpandableListView> refreshView) {
                page = getResources().getInteger(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.INTEGER, ResIdName.DEFAULT_PAGE));
                baseCommExpandableListAdapter.clear();
                loadingData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ExpandableListView> refreshView) {
                loadingData();
            }
        });
        //子项点击事件
        pullExpandableListView.getRefreshableView().setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                onChildItemClick(baseCommExpandableListAdapter.getChild(groupPosition, childPosition), groupPosition, childPosition);
                return false;
            }
        });
        //分组点击事件
        pullExpandableListView.getRefreshableView().setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                onGroupItemClick(baseCommExpandableListAdapter.getGroup(groupPosition), groupPosition);
                return false;
            }
        });
        {//加载数据
            loadDataView.setVisibility(View.VISIBLE);
            if (NetWorkUtils.getNetworkStatus(BaseApplication.getAppContext())) {
                loadDataView.loadingData();
                loadingData();
            } else {
                loadDataView.netWorkError();
            }
        }
    }

    @Override
    public void stopRefreshView() {
        pullExpandableListView.onRefreshComplete();
        if (baseCommExpandableListAdapter.getGroupCount() > 0) {
            loadDataView.setVisibility(View.GONE);
        } else {
            if (headerView != null) {
                loadDataView.setVisibility(View.GONE);
            } else {
                loadDataView.setVisibility(View.VISIBLE);
                loadDataView.loadDataError();
            }
        }
    }
}
